﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;

using EnvDTE;

using FanaticToy.Core.Utils;

namespace FanaticToy.Core
{
    ///<summary>当前插件的主要业务核心模块</summary>
    public class ThisAddIn
    {
        private DTE _dte;

        ///<summary>管理器集合。</summary>
        //private Dictionary<string, DirectoryManager> _managers  = new Dictionary<string, DirectoryManager>();
        public DirectoryManager DirectoryManager;

        public ThisAddIn(DTE dte)
        {
            //Application application = new Application();

            _dte = dte;
            _dte.Events.SolutionEvents.Opened += SolutionEvents_Opened;
            SolutionEvents_Opened();
        }

        ///<summary>当前解决方案更改后。</summary>
        private void SolutionEvents_Opened()
        {
            //_managers.Clear();
            //var solutions 

            var solutionPath = _dte.Solution.FullName;
            DirectoryManager = null;
            try
            {
                DirectoryManager = new DirectoryManager(solutionPath);
                Debug.WriteLine($"ThisAddIn.SolutionEvents_Opened: {DirectoryManager}");
                //MessageBox.Show(DirectoryManager.ToString());
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine($"ThisAddIn.SolutionEvents_Opened: {ex.Message}");
                //MessageBox.Show(ex.Message);
            }

        }

        ///<summary>导航到指定模块的全部结果。</summary>
        public IEnumerable<KeyValuePair<string, CodeMetaData>> GetNagivateResultList()
        {
            var list = new List<KeyValuePair<string, CodeMetaData>>();

            if (DirectoryManager == null) return list;
            //var item = _dte.ActiveDocument.ProjectItem;
            var textdoc = _dte.ActiveDocument.Object("TextDocument") as TextDocument;
            var line = textdoc.Selection.CurrentLine;

            var docPath = _dte.ActiveDocument.FullName;

            // 获得节点的 对应的层 Id.
            var nodeId = DirectoryManager.GetNodeId(docPath);
            var node = DirectoryManager.GetNodeById(nodeId); // 查看节点。

            // 当前的定位特征。
            var locationId = node.Strategy.GetLocationIdentifier(docPath, line);
            if (locationId == null) return list;

            // 获得当前全部的方法。
            foreach (var target in node.LinkNodes)
            {
                var targetId = target.TargetName;
                var codeMetaDatas = DirectoryManager.GetCodeMetaDatas(targetId, locationId.FileId, locationId.MemberId);

                if (codeMetaDatas == null) continue;

                foreach (var codeMetaData in codeMetaDatas)
                {
                    list.Add(new KeyValuePair<string, CodeMetaData>(targetId, codeMetaData));
                }

            }

            return list;
            // 当我们知道了当前路径按照决策的 class 名, method 名后，就可以去切换内容了。
            //node.Strategy;
        }

        public void Navigate(string filePath, int lineId)
        {
            foreach (var document in _dte.Documents.OfType<Document>())
            {
                var docPath = document.Path;
                if (PathUtils.IsPathEqual(docPath, filePath))
                {
                    document.Activate();
                    GotoLine(lineId, document);
                    return;
                }
            }

            var newDoc =  _dte.Documents.Open(filePath);
            newDoc.Activate();
            GotoLine(lineId, newDoc);
        }

        private void GotoLine(int lineId, Document document)
        {
            if (lineId < 0) return;
            var textdoc = document.Object("TextDocument") as TextDocument;
            textdoc.Selection.GotoLine(lineId + 1, false);
        }

    }
}
